#include "http.h"
#include <cstring>

const char          THttpResponseTemplate::KStatusLine[]        =   "HTTP/1.0 %d %s\r\n";
const unsigned int  THttpResponseTemplate::KStatusLineLen       =   sizeof(THttpResponseTemplate::KStatusLine) / sizeof(THttpResponseTemplate::KStatusLine[0]) - 1;
const char          THttpResponseTemplate::KContentLength[]     =   "Content-Length: %d\r\n";
const unsigned int  THttpResponseTemplate::KContentLengthLen    =   sizeof(THttpResponseTemplate::KContentLength) / sizeof(THttpResponseTemplate::KContentLength[0]) - 1;

const char          THttpResponseTemplate::KServer[]            =   "Server: multih\r\n";
const unsigned int  THttpResponseTemplate::KServerLen           =   sizeof(THttpResponseTemplate::KServer) / sizeof(THttpResponseTemplate::KServer[0]) - 1;

const char          THttpResponseTemplate::KContentType[]       =   "Content-Type: text/html\r\n";
const unsigned int  THttpResponseTemplate::KContentTypeLen      =   sizeof(THttpResponseTemplate::KContentType) / sizeof(THttpResponseTemplate::KContentType[0]) - 1;

const char          THttpResponseTemplate::KConnectionClose[]   =   "Connection: Close\r\n";
const unsigned int  THttpResponseTemplate::KConnectionCloseLen  =   sizeof(THttpResponseTemplate::KConnectionClose) / sizeof(THttpResponseTemplate::KConnectionClose[0]) - 1;


static TStatusToMessage buildHttpStatusMeassages()
{
    TStatusToMessage    mapper(600, "");
    mapper[200] =   "OK";
    mapper[400] =   "Bad Request";
    mapper[404] =   "Not Found";
    mapper[501] =   "Method Not Implemented";
    return mapper;
}

const TStatusToMessage& GetHttpStatusMessages()
{
    static const TStatusToMessage   mapper(buildHttpStatusMeassages());
    return mapper;
}

bool THttpRequest::IsValid()
{
    return ( ((NULL != HttpVersion) && (0 != HttpVersionLen))
           &&
            ((NULL != Method) && (0 != MethodLen))
           &&
            ((NULL != Path) && (0 != PathLen)) );
}

THttpRequest::ESupportedMethods THttpRequest::GetMethod()
{
    if ((MethodLen == 3) && (0 == strncmp("GET", Method, 3))) {
        return GET;
    }
    else if ((MethodLen == 4) && (0 == strncmp("HEAD", Method, 4))) {
        return HEAD;
    }
    else {
        return UNSUPPORTED;
    }
}
